home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13096 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  47 lines

  1. Path: news.netam.net!alpha!bgc
  2. From: bgc@alpha.netam.net (The Bowling Green Connection)
  3. Newsgroups: comp.lang.c
  4. Subject: Can you printf a long
  5. Date: 3 Apr 1996 16:24:33 GMT
  6. Organization: NetAmerica, Bowling Green, KY.
  7. Message-ID: <4ju8o1$dc3@news.netam.net>
  8. NNTP-Posting-Host: alpha.netam.net
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I had some trouble with this code:
  12. (I'm using gcc on Digital Unix)
  13.  
  14. int main() {
  15.     long i;
  16.  
  17.     while(1) {
  18.     i++;
  19.     printf("%f\n", i);
  20.     }
  21.  
  22.     return 0;
  23. }
  24.  
  25. The numbers printed to the screen as 0.00000.
  26. (Nevermind that this is an infinite loop--I was piping the output to
  27. a file which made me run out of disk space. This is just for testing)
  28.  
  29. But when I changed %f to %d, the numbers printed correctly.
  30. I thought that %d had the same limitations as an int..that is,
  31. it could only print about 4 bits of information, whereas a long
  32. is capable of more (8 or 16 bits), so wouldn't %f (float) be able
  33. to better handle those long numbers?  And why did %d work
  34. correctly, even up to 634,000 (that's when my disk space ran out.. :))
  35.  
  36. Well, then I changed the "long i" declaration to "int i", and changed %f 
  37. to %d, and I got the SAME results!  The numbers went up to 634,000!
  38. I didn't think an int could handle this much!
  39.  
  40. Another strange thing is, I did a sizeof(int) and a sizeof(long), and they
  41. BOTH returned a 4!!!  That can't be right, can it??!
  42.  
  43. _______________________________bgc@bgcky.com___________________________________
  44. Edgar E. Easterly, IV   http://www.bgcky.com    Bowling Green, Kentucky   -=O=-
  45. "Except the Lord build thy house, ye labour in vain that build it; Except   *
  46. the Lord keep thy city, the watchman will awake but in vain." Psalms 127:1  |
  47.